from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, TextAreaField, SelectField, \
    DateTimeField, IntegerField, BooleanField, SubmitField
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError
from app.models import User

class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    remember_me = BooleanField('记住我')
    submit = SubmitField('登录')

class RegistrationForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(min=4, max=64)])
    email = StringField('邮箱', validators=[DataRequired(), Email()])
    password = PasswordField('密码', validators=[DataRequired(), Length(min=6)])
    password2 = PasswordField('确认密码', validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('注册')
    
    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user is not None:
            raise ValidationError('用户名已被使用')
    
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user is not None:
            raise ValidationError('邮箱已被使用')

class ActivityForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(max=100)])
    description = TextAreaField('简介', validators=[DataRequired()])
    content = TextAreaField('内容')
    location = StringField('地点', validators=[DataRequired(), Length(max=100)])
    region = SelectField('地区', choices=[
        ('all', '全国'),
        ('beijing', '北京市'),
        ('shanghai', '上海市'),
        ('guangzhou', '广州市'),
        ('shenzhen', '深圳市'),
        ('chengdu', '成都市')
    ], validators=[DataRequired()])
    start_time = DateTimeField('开始时间', format='%Y-%m-%d %H:%M', validators=[DataRequired()])
    end_time = DateTimeField('结束时间', format='%Y-%m-%d %H:%M', validators=[DataRequired()])
    max_participants = IntegerField('最大参与人数', validators=[DataRequired()])
    image = FileField('活动图片', validators=[FileAllowed(['jpg', 'png', 'jpeg'], '只允许图片文件')])
    is_featured = BooleanField('设为精选活动')
    status = SelectField('状态', choices=[
        ('upcoming', '即将开始'),
        ('ongoing', '进行中'),
        ('completed', '已完成')
    ], validators=[DataRequired()])
    submit = SubmitField('保存')

class CourseForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired(), Length(max=100)])
    description = TextAreaField('简介', validators=[DataRequired()])
    content = TextAreaField('内容')
    duration = StringField('时长', validators=[DataRequired(), Length(max=50)])
    difficulty = SelectField('难度', choices=[
        ('beginner', '初级'),
        ('intermediate', '中级'),
        ('advanced', '高级')
    ], validators=[DataRequired()])
    image = FileField('课程图片', validators=[FileAllowed(['jpg', 'png', 'jpeg'], '只允许图片文件')])
    video_url = StringField('视频链接', validators=[Length(max=200)])
    is_featured = BooleanField('设为精选课程')
    status = SelectField('状态', choices=[
        ('draft', '草稿'),
        ('published', '已发布'),
        ('archived', '已归档')
    ], validators=[DataRequired()])
    submit = SubmitField('保存')

# 其他表单类...